##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_company {} {
	set values [$::daidze_main get_values]
	set msg_list [dict create name "Company name is obligatory." ]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all company * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default company. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		foreach lval [list logo logo2 logo3] {
			set tmp [dict get $values $lval]
			if {$tmp ne ""} {
				set tmp_file [open "$tmp" r]
				fconfigure $tmp_file -translation binary
				set values [dict set values $lval [read $tmp_file]]
				close $tmp_file
			}
		}
		puts $values
		$::db insert_all company $values
		$::daidze_main load_main company company.xml $::main 1
	}
}

proc edit_company {table_var} {
 	set sdata [dict create]
 	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	if {[$my_tablelist curselection] ne ""} { 
		set vars {id name}
		set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $vars value $row {
			dict set sdata $key $value
		}
		
		set r [$::db select_all company * "id=\'[dict get $sdata id]\'"]
		set result [dict remove [dict get $r 0] logo logo2 logo3]
		$::daidze_main load_main company edit_company.xml $::main
		$::daidze_main update_form $result
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc update_company {} {
	set values [$::daidze_main get_values]

	set msg_list [dict create name "Company name is obligatory." ]
	set flags [list]

	foreach key_val [dict keys $msg_list] {
		if {[dict get $values $key_val] == ""} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "[dict get $msg_list $key_val]"]"
			lappend flags 0
		} else { lappend flags 1}
	} 

	if {[lsearch -not -exact $flags 1] == "-1"} {
		if {[dict get $values def] eq "1"} {
			if {[$::db select_all company * "def=\'1\'"] != ""} {
				tk_messageBox -icon info -type ok -message "[::msgcat::mc "There is already default company. This one will be stored as generic type."]"
				dict set values def 0
			}
		}
		foreach lval [list logo logo2 logo3] {
			puts "Lval : $lval"
			set tmp [dict get $values $lval]
			puts "Temp: $tmp"
			if {$tmp ne ""} {
				set tmp_file [open "$tmp" r]
				fconfigure $tmp_file -translation binary
				set values [dict set values $lval [read $tmp_file]]
				close $tmp_file
			}
		}
		# puts $values
		$::db update_all company $values
		$::daidze_main load_main company company.xml $::main 1
	}
}

proc company_info {} {
	set my_tablelist [dict get [$::daidze_main get_fdata] company]
	set values [$my_tablelist get [$my_tablelist curselection]]
	if {$values == ""} {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select company, please!"]"
	} else {
		set a [$::db select_all company * "id=[lindex $values 0]"]
		set print_data [dict get $a 0]
		set pdf [my_print new [file join $::models company_profile.xml] $print_data]
		$pdf destroy 
	}
}

